package leetcode.D200.T121;

public class Solution {
    // 一刷
    /*public int maxProfit(int[] prices) {
        int profit = 0;
        for(int i=1; i<prices.length; ++i) {
            profit = Math.max(profit, prices[i] - prices[i-1]);
            prices[i] = Math.min(prices[i-1], prices[i]);
        }
        return profit;
    }*/

    // 二刷
    /*public int maxProfit(int[] prices) {
        int dp0 = 0, dp1 = Integer.MIN_VALUE;
        for(int i=0; i<prices.length; ++i) {
            dp0 = Math.max(dp0, dp1+prices[i]);
            dp1 = Math.max(dp1, -prices[i]);
        }
        return dp0;
    }*/

    // 三刷
    public int maxProfit(int[] prices) {
        int dp0 = 0, dp1 = -prices[0];
        for(int i=0; i<prices.length; ++i) {
            dp0 = Math.max(dp0, dp1+prices[i]);
            dp1 = Math.max(dp1, -prices[i]);
        }
        return dp0;
    }
}
